home *** CD-ROM | disk | FTP | other *** search
/ Amiga Game-Power / Amiga Game-Power.iso / power games ii / tetrix / ilbm2c / ilbm2c.c < prev    next >
C/C++ Source or Header  |  1994-05-20  |  7KB  |  249 lines

  1. /*  ILBM to C includable converter
  2.  
  3.     It seems as if there should be many versions of this type of utility.
  4.     When I went looking I couldn't find any, so I wrote this.  It was
  5.     compiled with Manx 3.4.
  6.     
  7.     This program reads in an IFF ILBM file and outputs a file you can include
  8.     in your C program.  It does not handle files with a real mask (although
  9.     it would be easy to incorporate that).  There should be a short demo
  10.     program with this to show how to use the file this creates.
  11.     
  12.     To use this program: ILBM2C infile outfile
  13.     
  14.     (This program is NOT very fast at reading large ILBM files. It has a
  15.     tendency to sit and churn for quite a while.)
  16.     
  17.     I have decided to put this program into the public domain.  Feel free to
  18.     do with it what you want, but please leave enough of this original header
  19.     that it is obvious where the program came from and what it is for.
  20.     
  21.     If you have comments, questions or spare cash, send them to:
  22.     
  23.         Tim Kemp
  24.         P.O. Box 23101
  25.         Columbus, Ohio 43223
  26. */
  27.  
  28.  
  29. #include <stdio.h>
  30. #include <fcntl.h>
  31.  
  32. #define MakeID(a,b,c,d)  ( (LONG)(a)<<24L | (LONG)(b)<<16L | (c)<<8 | (d) )
  33.  
  34. #define ID_ILBM MakeID('I','L','B','M')
  35. #define ID_BMHD MakeID('B','M','H','D')
  36. #define ID_CMAP MakeID('C','M','A','P')
  37. #define ID_BODY MakeID('B','O','D','Y')
  38. #define ID_FORM MakeID('F','O','R','M')
  39.  
  40. typedef unsigned int UWORD;
  41. typedef int WORD;
  42. typedef unsigned char UBYTE;
  43. typedef UBYTE Masking;
  44. typedef UBYTE Compression;
  45. typedef long LONG;
  46. extern int errno;
  47.  
  48. typedef struct {
  49.     UWORD w, h;                    /* raster width & height in pixels */
  50.     WORD  x, y;                    /* position for this image */
  51.     UBYTE nPlanes;                 /* # source bitplanes */
  52.     Masking masking;               /* masking technique */
  53.     Compression compression;       /* compression algoithm */
  54.     UBYTE pad1;                    /* UNUSED.  For consistency, put 0 here.*/
  55.     UWORD transparentColor;        /* transparent "color number" */
  56.     UBYTE xAspect, yAspect;        /* aspect ratio, a rational number x/y */
  57.     WORD  pageWidth, pageHeight;   /* source "page" size in pixels */
  58.     } BitMapHeader;
  59.  
  60. BitMapHeader bmh;
  61.  
  62. /* RowBytes computes the number of bytes in a row, from the width in pixels.*/
  63. #define RowBytes(w)   (((w) + 15) >> 4 << 1)
  64.  
  65. int infile,bpl;
  66. long bpp;
  67. unsigned char *layer[16],*AllocMem();
  68. FILE *outfile,*fopen();
  69.  
  70. main(argc,argv)
  71. int argc;
  72. char *argv[];
  73. {
  74.     long id,length,open();
  75.     int i;
  76.     unsigned char cmap[300];
  77.     
  78.     printf("IFF ILBM to C includable file converter  12/23/87 by Tim Kemp\n\n");
  79.     for (i=0; i<16; i++) layer[i] = 0;
  80.     if (argc<2)
  81.     {
  82.         printf("Usage : %s infile outfile\n",argv[0]);
  83.         exit(0);
  84.     }
  85.     infile = open(argv[1],O_RDONLY);
  86.     if (infile == -1)
  87.     {
  88.         printf("Can't open %s\n",argv[1]);
  89.         exit(1);
  90.     }
  91.     if (argc<3) outfile = stdout;
  92.     else
  93.     {
  94.         outfile = fopen(argv[2],"w");
  95.         if (!outfile)
  96.         {
  97.             printf("Couldn't open %s\n",argv[2]);
  98.             cleanup();
  99.         }
  100.     }
  101.     read(infile,&id,sizeof(long));
  102.     if (id != ID_FORM) badfile();
  103.     read(infile,&length,sizeof(long));
  104.     read(infile,&id,sizeof(long));
  105.     if (id != ID_ILBM) badfile();
  106.     bmh.w = 0;
  107.     length = 1;
  108.     while (!errno & length>0)
  109.     {
  110.         read(infile,&id,sizeof(long));
  111.         read(infile,&length,sizeof(long));
  112.         switch (id)
  113.         {
  114.             case ID_BMHD :
  115.                 read(infile,&bmh,sizeof(BitMapHeader));
  116.                 if (bmh.compression > 1)
  117.                 {
  118.                     printf(
  119.                     "Sorry, this program does not support #%d compression.\n",
  120.                     bmh.compression);
  121.                     cleanup();
  122.                 }
  123.                 if (bmh.compression) printf("this file is compressed.\n");
  124.                 if ((bmh.masking!=0) && (bmh.masking!=2))
  125.                 {
  126.                     printf("Sorry this program does not support masks.\n");
  127.                     cleanup();
  128.                 }
  129.                 bpl = RowBytes(bmh.w);
  130.                 bpp = bpl * bmh.h;
  131.                 for (i=0; i<bmh.nPlanes; i++)
  132.                 {
  133.                     if (!(layer[i] = AllocMem(bpp,0L)))
  134.                     {
  135.                         printf("Not enough memory.\n");
  136.                         cleanup();
  137.                     }
  138.                 }
  139.                 
  140.                 fprintf(outfile,"#define %s_width %u\n",argv[1],bmh.w);
  141.                 fprintf(outfile,"#define %s_height %u\n",argv[1],bmh.h);
  142.                 fprintf(outfile,"#define %s_num_planes %d\n",
  143.                     argv[1],bmh.nPlanes);
  144.                 fprintf(outfile,"#define %s_words_per_plane %ld\n\n",
  145.                     argv[1],bpp>>1);
  146.                 break;
  147.                 
  148.             case ID_CMAP :
  149.                 read(infile,cmap,(int)length);
  150.                 fprintf(outfile,"UWORD %s_colortable[] = {\n",argv[1]);
  151.                 for(i=0; i<length; i+=3)
  152.                 {
  153.                     if (i>0) fprintf(outfile,",\n");
  154.                     fprintf(outfile,"    0x%03x",
  155.                         ((int)(cmap[i]>>4)<<8) | ((cmap[i+1]>>4)<<4)
  156.                         | ((cmap[i+2]>>4)));
  157.                 }
  158.                 fprintf(outfile,"\n};\n\n");
  159.                 break;
  160.                 
  161.             case ID_BODY:
  162.                 readbody();
  163.                 fprintf(outfile,"UWORD %s_data[%d][%d][%d] = {\n",
  164.                     argv[1],bmh.nPlanes,bmh.h,bpl>>1);
  165.                 for (i=0; i<bmh.nPlanes; i++) printplane(layer[i]);
  166.                 fprintf(outfile,"};\n");
  167.                 cleanup();
  168.                     
  169.             default:
  170.                 lseek(infile,length,1);
  171.         }
  172.     }
  173.     cleanup();
  174. }
  175.  
  176. printplane(p)
  177. unsigned int p[];
  178. {
  179.     int i,j;
  180.     fprintf(outfile,"    {");
  181.     j = 100;
  182.     for (i=0; i<(bpp>>1); i++)
  183.     {
  184.         if (i>0) fprintf(outfile,",");
  185.         if (j>7)
  186.         {
  187.             fprintf(outfile,"\n        ");
  188.             j = 0;
  189.         }
  190.         fprintf(outfile,"0x%04x",p[i]);
  191.         j++;
  192.     }
  193.     fprintf(outfile,"\n    },\n");
  194.     return;
  195. }
  196.  
  197. readbody()
  198. {
  199.     int line,layr;
  200.     for (line=0; line < bmh.h; line++)
  201.         for (layr = 0; layr < bmh.nPlanes; layr++)
  202.         {
  203.             if (bmh.compression) readline(&(layer[layr][line*bpl]));
  204.             else read(infile,&(layer[layr][line*bpl]),bpl);
  205.         }
  206.     return;
  207. }
  208.  
  209. readline(addr)
  210. unsigned char addr[];
  211. {
  212.     char c,d;
  213.     int i,j;
  214.     
  215.     i = 0;
  216.     while (i<bpl)
  217.     {
  218.         read(infile,&c,1);
  219.         if (c>0)
  220.         {
  221.             read(infile,&addr[i],(int)c+1);
  222.             i += c+1;
  223.         }
  224.         else
  225.         {
  226.             read(infile,&d,1);
  227.             j = -c + 1;
  228.             for (;j>0; j--,i++) addr[i] = d;
  229.         }
  230.     }
  231.     return;
  232. }
  233.  
  234. cleanup()
  235. {
  236.     int i;
  237.     for (i=0; i<16; i++) if (layer[i]) FreeMem(layer[i],bpp);
  238.     close(infile);
  239.     if (outfile) fclose(outfile);
  240.     exit(0);
  241. }
  242.     
  243.     
  244. badfile()
  245. {
  246.         printf("File of wrong type");
  247.         cleanup();
  248. }
  249.